import Util.EsUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import pojo.testPOJO;

import java.util.List;

public class EsUtilTestMain {

    public static void main(String[] args) {

        //创建索引
//        boolean a = EsUtil.insertIndex("zyy_index");
//        System.out.println(a);

        //索引是否存在
//        boolean a = EsUtil.isExitsIndex("zyy_index_test1");
//        System.out.println(a);


        //删除索引
//        boolean a = EsUtil.deleteIndex("zyy_index_test");
//        System.out.println(a);

        //新增/修改文档
//        Map<String,Object> paramMap = new HashMap<String,Object>();
//        paramMap.put("name","张22222三");
//        paramMap.put("age",16);
//        paramMap.put("sal",12500.00);
//        paramMap.put("addr","杭州市西湖区");
//        boolean a = EsUtil.insertOrUpdateDocument("zyy_index_test","2",paramMap);
//        System.out.println(a);

        //修改文档信息
//        Map<String,Object> paramMap = new HashMap<String,Object>();
//        paramMap.put("name","李四");
//        paramMap.put("age",16);
//        paramMap.put("sal",12500.00);
//        paramMap.put("addr","杭州市西湖区");
//        boolean a = EsUtil.updateDocument("zyy_index_test","1",paramMap);
//        System.out.println(a);

        //

        //删除文档信息
//        boolean a = EsUtil.deleteDocument("zyy_index_test","2");
//        System.out.println(a);


//        简单批量新增
//        List<Object> list = new ArrayList<>();
//        list.add(new testPOJO("张三1",11,10000.11,"西湖区别墅122号"));
//        list.add(new testPOJO("张三2",12,10000.12,"西湖区别墅123号"));
//        list.add(new testPOJO("张三3",13,10000.13,"西湖区别墅124号"));
//        list.add(new testPOJO("张三4",14,10000.14,"西湖区别墅125号"));
//        list.add(new testPOJO("张三5",15,10000.15,"西湖区别墅126号"));
//        list.add(new testPOJO("张三6",16,10000.16,"西湖区别墅127号"));
//        list.add(new testPOJO("张三7",17,10000.17,"西湖区别墅128号"));
//        list.add(new testPOJO("张三8",18,10000.18,"西湖区别墅129号"));
//        boolean b = EsUtil.simplePatchInsert("zyy_index",list,10L);
//        System.out.println(b);


        //判断文档是否存在
//        boolean a = EsUtil.isExistsDocument("zyy_index_test","1");
//        System.out.println(a);

        //根据id查询文档
//        testPOJO pojo = EsUtil.selectDocumentById("zyy_index","xLP9VnoBngRFmC_Ti0ak",testPOJO.class);
//        System.out.println(pojo);

        //根据查询条件获取数据集
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        builder.query(QueryBuilders.matchQuery("name","张三1"));
//        List<testPOJO> list = EsUtil.selectDocumentList("zyy_index",builder,testPOJO.class);
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }

        //高亮查询
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        builder.query(QueryBuilders.matchPhraseQuery("name","张三1")); //使用termQuery查询是一个BUG,无法查询匹配,可以使用matchPhraseQuery替代,该方法表示不使用分词,而是以一个短语的形式查询
//        List<testPOJO> list = EsUtil.selectDocumentListHighLight("zyy_index",builder,testPOJO.class,"name");
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }


        //分页查询
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        builder.from(0);//起始数据
//        builder.size(10);//查询条数
//        builder.query(QueryBuilders.matchPhraseQuery("name","张三1"));
//        List<testPOJO> list = EsUtil.selectDocumentList("zyy_index",builder,testPOJO.class);
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }


//        //排序查询
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        builder.sort("age", SortOrder.DESC);
////        builder.sort("age", SortOrder.ASC);
//        builder.query(QueryBuilders.matchPhraseQuery("name","张三1"));
//        List<testPOJO> list = EsUtil.selectDocumentList("zyy_index",builder,testPOJO.class);
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }

        //过滤字段查询
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        String[] excludes = {};
//        String[] includes = {"name","age"};
//        builder.fetchSource(includes,excludes);
//        builder.query(QueryBuilders.matchPhraseQuery("name","张三1"));
//        List<testPOJO> list = EsUtil.selectDocumentList("zyy_index",builder,testPOJO.class);
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }

        //Bool查询
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        //必须包含
//        boolQueryBuilder.must(QueryBuilders.matchQuery("addr","male"));
//        //一定不包含
//        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name","zhangsan"));
//        //可能包含
//        boolQueryBuilder.should(QueryBuilders.matchQuery("age",40));
//        builder.query(boolQueryBuilder);
//        List<testPOJO> list = EsUtil.selectDocumentList("zyy_index",builder,testPOJO.class);
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }

        //范围查询
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        RangeQueryBuilder rangeQueryBuilder  = QueryBuilders.rangeQuery("age");
//        //大于等于
//        rangeQueryBuilder.gte(20);
//        //大于
//        rangeQueryBuilder.gt(20);
//        //小于
//        rangeQueryBuilder.lt(40);
//        //小于等于
//        rangeQueryBuilder.lte(40);
//
//        builder.query(rangeQueryBuilder);
//        List<testPOJO> list = EsUtil.selectDocumentList("zyy_index",builder,testPOJO.class);
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }

        //模糊查询
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name","张三").fuzziness(Fuzziness.ONE);
//        builder.query(fuzzyQueryBuilder);
//        List<testPOJO> list = EsUtil.selectDocumentList("zyy_index",builder,testPOJO.class);
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }

        //聚合查询-max
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        builder.aggregation(AggregationBuilders.max("maxAge").field("age"));
//        List<testPOJO> list = EsUtil.selectDocumentList("zyy_index",builder,testPOJO.class);
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }

        //聚合查询terms
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        builder.aggregation(AggregationBuilders.terms("age_groupBy").field("age"));
//        List<testPOJO> list = EsUtil.selectDocumentList("zyy_index",builder,testPOJO.class);
//        for (testPOJO test : list) {
//            System.out.println(test);
//        }

    }


}
